package com.hy.ruiji.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.ruiji.common.R;
import com.hy.ruiji.common.Status;
import com.hy.ruiji.enyity.Employee;
import com.hy.ruiji.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录信息
     * @param request 把用户信息放到session里面
     * @param employee 前端传入的实体类信息
     * @return 返回登录信息给前端
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
       //调用登录方法
        R<Employee> login = employeeService.login(employee);
        if(login.getData()==null){
            return login;
        }
        request.getSession().setAttribute("employee",login.getData().getId());
        return login;
    }
    /**
     * 退出功能
     * @param request 获取session，退出时清除
     * @return 返回清除信息
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功！");
    }

    /**
     * 新增员工
     * @param employee 前端传入的值
     * @param request 获取当前的session值
     * @return 返回添加结果
     */
    @PostMapping
    public R<String> saveEmployee(HttpServletRequest request, @RequestBody Employee employee){
        log.info("新增员工{}",employee.toString());
        return employeeService.saveEmployee(request,employee);
    }

    /**
     * 对员工进行分页查询
     * @param page 传入的页数
     * @param pageSize 传入的每页所需的条数
     * @param name 模糊查询的字段
     * @return 返回查询结果
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    /**
     * 修改员工信息
     * @param request 获取session的id
     * @param employee 获取需要修改的信息
     * @return 返回修改的结果
     */
    @PutMapping
    public R<String> updateEmployee(HttpServletRequest request,@RequestBody Employee employee){
        //Long empId =(Long) request.getSession().getAttribute(Status.SESSION_USER);
        employeeService.updateById(employee);
        return R.success("修改成功！");
    }

    /**
     * 根据id查询员工信息
     * @param id 需要查询的员工id
     * @return 返回查询结果
     */
    @GetMapping("/{id}")
    public R<Employee> queryEmployeeById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
